package com.ogawa.meiditest.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.midea.iot.sdk.MideaSDK
import com.ogawa.meiditest.bean.request.TokenBean
import com.ogawa.meiditest.databinding.ActivityMainBinding
import com.ogawa.meiditest.network.ApiService
import com.ogawa.meiditest.network.RetrofitManager
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext

class MainActivity : AppCompatActivity() {

    private val job = Job()
    private val main: CoroutineContext = Dispatchers.Main + job
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        GlobalScope.launch {
            val apiService = RetrofitManager.INSTANCE.createService(ApiService::class.java)
            val token = apiService.getToken(TokenBean())
            MideaSDK.INSTANCE.setAccessToken(token.data.accessToken)
            withContext(main) {
                Toast.makeText(this@MainActivity, "授权成功", Toast.LENGTH_SHORT).show()
            }
        }
        binding.btnConnectNet.setOnClickListener {
            startActivity(Intent(this, NetConnectActivity::class.java))
        }
        Log.e("tag","---------------------------->>${MideaSDK.INSTANCE.mInitialized}")
    }

    override fun onDestroy() {
        super.onDestroy()
        job.cancel()
    }
}